package com.github.pig.admin.service.notificationInfo.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.notificationInfo.NotificationInfoMapper;
import com.github.pig.admin.mapper.notificationInfo.NotificationUserMapper;
import com.github.pig.admin.model.entity.SysFile;
import com.github.pig.admin.model.zhgl.notification.NotificationInfo;
import com.github.pig.admin.model.zhgl.notification.NotificationUser;
import com.github.pig.admin.service.notificationInfo.NotificationInfoService;
import com.github.pig.admin.service.sysFile.SysFileService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.admin.util.CommonEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.List;

/**
 * <p>
 * 通知公告 服务实现类
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-05
 */
@Service
public class NotificationInfoServiceImpl extends ServiceImpl<NotificationInfoMapper, NotificationInfo> implements NotificationInfoService {

    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private NotificationInfoMapper INotificationInfoMapper;
    @Autowired
    private NotificationUserMapper notificationUserMapper;


    /**
     * 分页查询信息（通知公告）
     *
     * @return 分页对象
     */
    /*@Override
    public Page<NotificationInfo> indexPage(Map<String, Object> params) {
        Page<NotificationInfo> page = new Page<NotificationInfo>(Integer.parseInt(params.get("page").toString()),
                Integer.parseInt(params.get("limit").toString()));
        EntityWrapper<NotificationInfo> entityWrapper = new EntityWrapper<NotificationInfo>();
        entityWrapper.eq("community_id",params.get("communityId"));
        entityWrapper.orderBy("create_date", false);
        Page<NotificationInfo> indexPage = this.selectPage(page, entityWrapper);
        List<NotificationInfo> list = indexPage.getRecords();
        if (list.size() > 0) {
            for (NotificationInfo notificationInfo : list) {
                if (StringUtils.isNotBlank(notificationInfo.getPicTop())) { //顶部图片
                    SysFile sysFile = sysFileService.selectById(notificationInfo.getPicTop());
                    ByteArrayOutputStream outputStream = null;
                    InputStream inputStream = null;
                    byte[] data = null;
                    try {
                        inputStream = new FileInputStream(sysFile.getDocPath());
                        data = new byte[inputStream.available()];
                        inputStream.read(data);
                        inputStream.close();
                        BASE64Encoder encoder = new BASE64Encoder();

                        notificationInfo.setPicTopPath(encoder.encode(data));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return indexPage;
    }*/

    @Override
    public Page<NotificationInfo> indexPage(Page<NotificationInfo> page,NotificationInfo notificationInfo) {
        List<NotificationInfo> pageList = INotificationInfoMapper.selectPages(page,notificationInfo);
        if (notificationInfo.getType().equals(CommonEnum.EVENT.getStatus())){ //活动
            if (pageList.size() > 0){
                for (NotificationInfo info : pageList) {
                    EntityWrapper<NotificationUser> entityWrapper = new EntityWrapper<>();
                    entityWrapper.eq("notification_id",info.getId());
                    List<NotificationUser> users = notificationUserMapper.selectList(entityWrapper);
                    if (users.size() > 0){
                       // info.setUserList(users);
                    }
                }
            }
        }
        return page.setRecords(pageList);
    }

    @Override
    public AjaxJson get(String id) {
        AjaxJson json = new AjaxJson();
        NotificationInfo notificationInfo = this.selectById(id);
        if (notificationInfo != null){
            if (StringUtils.isNotBlank(notificationInfo.getPicTop())) { //顶部图片
                SysFile sysFile = sysFileService.selectById(notificationInfo.getPicTop());
                ByteArrayOutputStream outputStream = null;
                InputStream inputStream = null;
                byte[] data = null;
                try {
                    inputStream = new FileInputStream(sysFile.getDocPath());
                    data = new byte[inputStream.available()];
                    inputStream.read(data);
                    inputStream.close();
                    BASE64Encoder encoder = new BASE64Encoder();

                    //notificationInfo.setPicTopPath(encoder.encode(data));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            /*if (StringUtils.isNotBlank(notificationInfo.getPicBody())) { //正文图片
                String[] paths = notificationInfo.getPicBody().split(",");
                if (paths.length > 0){
                    String[] body = new String[paths.length];
                    for (int i = 0; i < paths.length; i++) {
                        SysFile sysFile = sysFileService.selectById(paths[i]);
                        ByteArrayOutputStream outputStream = null;
                        InputStream inputStream = null;
                        byte[] data = null;
                        try {
                            inputStream = new FileInputStream(sysFile.getDocPath());
                            data = new byte[inputStream.available()];
                            inputStream.read(data);
                            inputStream.close();
                            BASE64Encoder encoder = new BASE64Encoder();
                            body[i] = encoder.encode(data);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    notificationInfo.setPicBody(Arrays.toString(body));
                }
            }*/
            json.setMsg("成功。");
            json.setSuccess(true);
            json.setObj(notificationInfo);
        }else {
            json.setMsg("操作有误，请稍后尝试。");
            json.setSuccess(false);
        }
        return json;
    }
}
